﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// shuaxin 的摘要说明
    /// </summary>
    public class shuaxin : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "refresh_gongying": //刷新供应信息
                    refresh_gongying(context);
                    break;
                case "refresh_qiugou": //刷新求购信息
                    refresh_qiugou(context);
                    break;
            }
        }

        #region 刷新供应信息=====================================
        private void refresh_gongying(HttpContext context)
        {
            string _user_name = DTRequest.GetString("txtUserName");
            string _user_pwd = DTRequest.GetString("txtPassword");

            if (string.IsNullOrEmpty(_user_name) || string.IsNullOrEmpty(_user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            Model.users userModel = new BLL.users().GetModel(_user_name, _user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            //if (userModel.group_id < 2)
            //{
            //    context.Response.Write("{\"status\":0, \"msg\":\"抱歉，您没有刷新权限，请升级会员权限！\"}");
            //    return;
            //}

            int article_id = DTRequest.GetQueryInt("article_id");
            if (article_id == 0)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"未指定内容ID！\"}");
                return;
            }
            BLL.xinxi bll = new BLL.xinxi();
            Model.xinxi model = bll.GetModel(article_id);
            if (model == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"抱歉，未找到您的查找的内容！\"}");
                return;
            }
            //判断该用户是否有删除权限
            if (model.user_id!=userModel.id)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"抱歉，您没有操作该内容的权限！\"}");
                return;
            }

            //####添加短时间内发布限制 180秒内不能发布超过3条 ，暂不限类型
            //####Demon 2020-03-09
            //int xcount= bll.GetNumByUser_idAndType_Idandtime(userModel.id, _type_id, 180);//限制类型，当前添加的类型 180秒内不能超过3条
            int xcount = bll.GetNumByUser_idAndType_Idandtime(userModel.id, 0, 180);
            Utils.WriteTextLog("refresh_gongying_" + _user_name, "username:" + _user_name + ",pwd:" + _user_pwd + "/r/n180秒内添加得信息数量：" + xcount + "/r/n 类型 " , DateTime.Now);
            if (xcount >= 3)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"操作过于频繁,请稍后再试\"}");
                return;
            }

            //#####根据用户会员级别查看会员委托求购信息详情电话的查看次数
            //##### 2018 11 01
            BLL.users blluser = new BLL.users();
            BLL.user_groups bllgroup = new BLL.user_groups();
            int userViewCount = bllgroup.GetupdateNum(userModel.group_id);
            string tips = "";
            //###普通会员刷新
            //###认证会员刷新
            //###钻石会员刷新
            //### Demon 2018-11-1
            if (userModel.readtime != null && userModel.refreshtime.Date == DateTime.Now.Date)
            {
                if (userModel.refreshcount >= userViewCount)
                {
                    ///tips = "今日刷新次数已用完，请省级VIP";

                    if (userModel.group_id < 3) { tips = "今日刷新次数已用完，升级VIP可获得更多刷新次数！"; }
                    else { tips = "今日刷新次数已用完！"; }
                    context.Response.Write("{\"status\":0, \"msg\":\"" + tips + "\"}");
                    return;
                }
                blluser.UpdateField(userModel.id, "refreshcount=refreshcount+1,refreshtime=getdate()");
            }
            else {
                blluser.UpdateField(userModel.id, "refreshcount=1,refreshtime=getdate()");
            }
            
            model.add_time = DateTime.Now;
            bool result = bll.Update(model);
            if (result)
            {
                context.Response.Write("{\"status\":1, \"msg\":\"刷新成功！\"}");
                return;
            }
            context.Response.Write("{\"status\":0, \"msg\":\"更新失败，请重试！\"}");
            return;
        }
        #endregion

        #region 刷新求购信息=====================================
        private void refresh_qiugou(HttpContext context)
        {
            string _user_name = DTRequest.GetString("txtUserName");
            string _user_pwd = DTRequest.GetString("txtPassword");

            if (string.IsNullOrEmpty(_user_name) || string.IsNullOrEmpty(_user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            Model.users userModel = new BLL.users().GetModel(_user_name, _user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            //if (userModel.group_id < 2)
            //{
            //    context.Response.Write("{\"status\":0, \"msg\":\"抱歉，您没有刷新权限，请升级会员权限！\"}");
            //    return;
            //}

            int article_id = DTRequest.GetQueryInt("article_id");
            if (article_id == 0)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"未指定内容ID！\"}");
                return;
            }
            BLL.qiugou bll = new BLL.qiugou();
            Model.qiugou model = bll.GetModel(article_id);
            if (model == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"抱歉，未找到您的查找的内容！\"}");
                return;
            }
            //判断该用户是否有删除权限
            if (model.user_id != userModel.id)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"抱歉，您没有操作该内容的权限！\"}");
                return;
            }
            //#####根据用户会员级别查看会员委托求购信息详情电话的查看次数
            //##### 2018 11 01
            BLL.users blluser = new BLL.users();
            BLL.user_groups bllgroup = new BLL.user_groups();
            int userViewCount = bllgroup.GetupdateNum(userModel.group_id);
            string tips = "";
            //###普通会员刷新
            //###认证会员刷新
            //###钻石会员刷新
            //### Demon 2018-11-1
            if (userModel.readtime != null && userModel.refreshtime.Date == DateTime.Now.Date)
            {
                if (userModel.refreshcount >= userViewCount)
                {
                    ///tips = "今日刷新次数已用完，请省级VIP";

                    if (userModel.group_id < 3) { tips = "今日刷新次数已用完，升级VIP可获得更多刷新次数！"; }
                    else { tips = "今日刷新次数已用完！"; }
                    context.Response.Write("{\"status\":0, \"msg\":\"" + tips + "\"}");
                    return;
                }
            }
            blluser.UpdateField(userModel.id, "refreshcount=refreshcount+1,refreshtime=getdate()");
            model.add_time = DateTime.Now;
            bool result = bll.Update(model);
            if (result)
            {
                context.Response.Write("{\"status\":1, \"msg\":\"刷新成功！\"}");
                return;
            }
            context.Response.Write("{\"status\":0, \"msg\":\"更新失败，请重试！\"}");
            return;
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}